<?php

namespace App\Http\Controllers;

use App\Models\ActionLog;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public $baseUrl;

    public function __construct(Request $request)
    {
        $this->baseUrl      ='http://'.$_SERVER['SERVER_NAME'];
        $current_url        = $request->path();
        $current_url_arr    = explode('/',$current_url);

        if (strtolower($current_url_arr[0])  == 'admin'){
            $id = 0;
            if (!empty(Auth::id())){
                $id             = Auth::id();
            }
            $data               = [
              'uid'             => $id,
              'action_path'     => $current_url,
              'action_name'     =>$this->getActionName($current_url_arr[0].'/'.$current_url_arr[1].'/'.$current_url_arr[2])
            ];
            $this->addActionLog($data);
        }

    }

    public function addActionLog($data)
    {
        return ActionLog::add($data);
    }


    public function getActionName(string $path)
    {
        $actionMap = [
            'admin/user/index'                              => '用户列表',
            'admin/user/destroy'                            => '删除用户',
            'admin/user/disabled'                           => '禁用/启用用户',
            'admin/user/rebateBenefit'                      => '用户分销收益列表',
            'admin/user/rebateDevote'                       => '用户分销贡献列表',
            'admin/rebate/index'                            => '系统分销利率列表',
            'admin/rebate/edit'                             => '修改系统分销利率',
            'admin/RedBag/index'                            => '红包列表',
            'admin/RedBag/edit'                             => '修改单个红包限制金额',
            'admin/RechargeCommission/index'                => '代充值列表',
            'admin/RechargeCommission/edit'                 => '代充值比例修改',
            'admin/vip/index'                               => '会员卡列表',
            'admin/vip/modifyVipLimit'                      => '会员卡临界值修改',
            'admin/vip/disabled'                            => '禁用/启用会员卡',
            'admin/vip/add'                                 => '新增会员卡',
            'admin/vip/edit'                                => '修改会员卡',
            'admin/vip/unBind'                              => '解绑会员卡',
            'admin/FeedBack/index'                          => '反馈列表',
            'admin/copartner/apply'                         => '合伙人申请列表',
            'admin/copartner/doapply'                       => '合伙人申请列表',
            'admin/property/index'                          => '规格列表',
            'admin/property/create'                         => '规格创建页',
            'admin/property/update'                         => '规格编辑页',
            'admin/property/docreate'                       => '规格创建',
            'admin/property/delete'                         => '规格编辑',
            'admin/spu/index'                               => '商品列表',
            'admin/spu/market'                              => '商品的上/下架',
            'admin/spu/create'                              => '商品创建页面',
            'admin/spu/edit'                                => '商品编辑页面',
            'admin/spu/docreate'                            => '商品创建',
            'admin/spu/doedit'                              => '商品编辑',
            'admin/spu/del'                                 => '商品删除',
            'admin/spu/uploadImg'                           => '商品图片上传',
            'admin/spu/delImg'                              => '商品图片删除',
            'admin/coupon/rule'                             => '优惠券规则列表',
            'admin/coupon/create_rule_form'                 => '优惠券规则创建页面',
            'admin/coupon/edit_rule_form'                   => '优惠券规则编辑页面',
            'admin/coupon/createrule'                       => '优惠券规则创建',
            'admin/coupon/editrule'                         => '优惠券规则编辑',
            'admin/coupon/delterule'                        => '优惠券规则删除',
            'admin/coupon/coupon_list'                      => '优惠券列表',
            'admin/coupon/create_coupon_form'               => '优惠券创建页面',
            'admin/coupon/edit_coupon_form'                 => '优惠券编辑页面',
            'admin/coupon/create_coupon'                    => '优惠券创建',
            'admin/coupon/edit_coupon'                      => '优惠券编辑',
            'admin/coupon/delete_coupon'                    => '优惠券删除',
            'admin/recharge/index'                          => '充值优惠配置页面',
            'admin/recharge/save'                           => '充值优惠配置保存',
            'admin/cash/index'                              => '提现列表页',
            'admin/cash/set_index'                          => '提现配置页面',
            'admin/cash/docash'                             => '提现',
            'admin/cash/save_set'                           => '提现配置保存',
            'admin/system/actionlog'                        => '操作日志',
            'admin/system/loginlogs'                        => '操作日志',
            'admin/system/aboutus'                          => '关于我们',
            'admin/system/aboutus_edit'                     => '编辑关于我们',
            'admin/banner/index'                            => '轮播图列表',
            'admin/banner/destroy'                          => '轮播图删除',
            'admin/banner/create'                           => '轮播图创建页面',
            'admin/banner/store'                            => '轮播图创建',
            'admin/banner/edit'                             => '轮播图编辑页面',
            'admin/banner/update'                           => '轮播图更新',
            'admin/goods/category'                            => '分类列表页',
            'admin/goods/create'                              => '分类创建页面',
            'admin/goods/cate_store'                          => '分类创建',
            'admin/goods/cate_destory'                        => '分类删除',
            'admin/goods/cate_edit'                           => '分类编辑页面',
            'admin/goods/cate_update'                         => '分类更新',
            'admin/comment/index'                             => '评论列表',
            'admin/comment/destory'                           => '评论删除',
            'admin/order/index'                               => '订单列表',
            'admin/order/detail'                              => '订单详情',
        ];
        $action = $actionMap[$path] ?? '';
        return $action;
    }



}
